package day2;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Date;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import utility.JdbcUtils;


public class ParameterMetaTest {
	public static void main(String[] args) throws SQLException {
		String sql = "select * from user where name=? and  money > ? ";
		fillParameters(sql, new Object[]{"a",1});
	}
	
	private static void fillParameters(String sql,Object[] objects) throws SQLException {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		try {
			// 2.建立连接
			con = JdbcUtils.getConnection();
			// 3.创建语句
			
			st = con.prepareStatement(sql);
			//getParameterType(int param)：获得指定参数的sql类型，MySQL数据库驱动不支持
			ParameterMetaData pmd=st.getParameterMetaData();
			System.out.println(pmd);
			int count=pmd.getParameterCount();
			System.out.println(count);
		/*	for(int i=1;i<=count;i++){
				System.out.println(pmd.getParameterClassName(i)+"\t");
				System.out.println(pmd.getParameterType(i)+"\t");
				System.out.println(pmd.getParameterTypeName(i)+"\t");
			}*/
			
			for(int i=1;i<=objects.length;i++){
				st.setObject(i, objects[i-1]);
			}
			
			rs=st.executeQuery();
			while(rs.next()){
				System.out.println("name = "+rs.getString("name"));
			}

		}finally{
			JdbcUtils.free(rs, st, con);
			
		}
	}
}
